﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PepsiCo.Business
{
    public partial class pepsi_Contact
    {
        public int? GetOperatorID()
        {
            using (PepsiCoDBDataContext db = new PepsiCoDBDataContext(LocalSetting.ConnectionString))
            {
                OperatorPrefixCode obj = db.OperatorPrefixCodes.FirstOrDefault(p => this.PhoneNumber.IndexOf(p.prefix_code) == 0);
                if (obj != null) return obj.operator_id;
                return null;
            }
        }

        public bool ValidatePhoneNumber(string phoneNumber)
        {
            using (PepsiCoDBDataContext db = new PepsiCoDBDataContext(LocalSetting.ConnectionString))
            {
                if (db.OperatorPrefixCodes.Any(p => phoneNumber.IndexOf(p.prefix_code) == 0
                            && phoneNumber.Length >= p.min_length
                            && phoneNumber.Length <= p.max_length)
                    )
                    return true;
                return false;
            }
        }

        public bool ValidateListOfPhoneNumber(string[] phoneNumber)
        {
            using (PepsiCoDBDataContext db = new PepsiCoDBDataContext(LocalSetting.ConnectionString))
            {
                foreach (string item in phoneNumber)
                {
                    string phone = NormalizedPhoneNumber(item);
                    if (!db.OperatorPrefixCodes.Any(p => phone.IndexOf(p.prefix_code) == 0
                                && phone.Length >= p.min_length
                                && phone.Length <= p.max_length)
                        )
                        return false;
                }
                return true;
            }
        }

        public static string NormalizedPhoneNumber(string phoneNumber)
        {
            string normalPhoneNumber = string.Empty;
            foreach (char item in phoneNumber)
            {
                if (item >= '0' && item <= '9')
                    normalPhoneNumber += item;
            }
            //Kiểm tra 2 ký tự đầu, nếu không phải là 84 thì xử lý
            if (!normalPhoneNumber.StartsWith("84"))
            {
                //Kiểm tra 1 ký tự đầu, nếu là ký tự 0 thì cắt đi
                if (normalPhoneNumber.StartsWith("0"))
                {
                    normalPhoneNumber = normalPhoneNumber.Substring(1);
                }
                //Thêm ký tự 84 vào trước
                normalPhoneNumber = "84" + normalPhoneNumber;
            }
            return normalPhoneNumber;
        }

        public static bool CheckValidPhoneNumber(string[] phoneNumber)
        {
            using (PepsiCoDBDataContext db = new PepsiCoDBDataContext(LocalSetting.ConnectionString))
            {
                foreach (string item in phoneNumber)
                {
                    string phone = NormalizedPhoneNumber(item);
                    if (!db.OperatorPrefixCodes.Any(p => phone.IndexOf(p.prefix_code) == 0
                                && phone.Length >= p.min_length
                                && phone.Length <= p.max_length)
                        )
                        return false;
                }
                return true;
            }
        }

        public static bool CheckExistsPhoneNumber(Guid? staffId, string[] phoneNumber)
        {
            for (int i = 0; i < phoneNumber.Length; i++)
                phoneNumber[i] = NormalizedPhoneNumber(phoneNumber[i]);
            using (PepsiCoDBDataContext db = new PepsiCoDBDataContext(LocalSetting.ConnectionString))
            {
                if (staffId.HasValue)
                {
                    if (db.pepsi_Contacts.Any(p => phoneNumber.Contains(p.PhoneNumber) && p.StaffID != staffId))
                        return true;
                }
                else
                {
                    if (db.pepsi_Contacts.Any(p => phoneNumber.Contains(p.PhoneNumber)))
                        return true;
                }
                return false;
            }
        }
    }
}
